#include "CToday.h"         //  class definition
#include <QDebug>           //  debug helpers
#include "todaywindow.h"


CToday::CToday( TodayWindow tday ) : CWorkingDay()
{
    //  Zero variables
    m_iActivity = Other;

    m_tTravelTimeStart;         //  Time started traveling
    m_tTravelTimeStop;          //  Time stopped traveling

    m_tDropTimeStart;           //  Time started dropping
    m_tDropTimeStop;            //  Time stopped dropping

    m_tCollectTimeStart;        //  Time started collecting
    m_tCollectTimeStop;         //  Time stopped collecting

    m_tDeliverTimeStart;        //  Time started delivering
    m_tDeliverTimeStop;         //  Time stopped delivering

    connect( &tday, SIGNAL(ActionStart(ACTIVITIES)), this, SLOT(OnActionStart(ACTIVITIES)));
}

CToday::CToday( QString buff ) : CWorkingDay( buff )
{
    //  Zero variables
    m_iActivity = Other;

    m_tTravelTimeStart;         //  Time started traveling
    m_tTravelTimeStop;          //  Time stopped traveling

    m_tDropTimeStart;           //  Time started dropping
    m_tDropTimeStop;            //  Time stopped dropping

    m_tCollectTimeStart;        //  Time started collecting
    m_tCollectTimeStop;         //  Time stopped collecting

    m_tDeliverTimeStart;        //  Time started delivering
    m_tDeliverTimeStop;         //  Time stopped delivering
}

CToday::~CToday()
{
    //  Zero variables
    m_iActivity = Other;

    m_tTravelTimeStart;         //  Time started traveling
    m_tTravelTimeStop;          //  Time stopped traveling

    m_tDropTimeStart;           //  Time started dropping
    m_tDropTimeStop;            //  Time stopped dropping

    m_tCollectTimeStart;        //  Time started collecting
    m_tCollectTimeStop;         //  Time stopped collecting

    m_tDeliverTimeStart;        //  Time started delivering
    m_tDeliverTimeStop;         //  Time stopped delivering
}

//----------------------------------
//  General controls/helpers
//----------------------------------
void        CToday::SetActivity( ACTIVITIES action )
{
    m_iActivity = action;
}

void        CToday::Start( ACTIVITIES action )
{
    if( action != Other )
        m_iActivity = action;

    switch( m_iActivity )
    {
    case Traveling:
        StartTraveling();
        break;

    case Dropping:
        StartDropping();
        break;

    case Collecting:
        StartCollecting();
        break;

    case Delivering:
        StartDelivering();
        break;

    default:
        qDebug() << "Unknown activity, nothing started";
        break;
    }
}

void        CToday::Stop( ACTIVITIES action )
{
    if( action != Other )
        m_iActivity = action;

    switch( m_iActivity )
    {
    case Traveling:
        StopTraveling();
        break;

    case Dropping:
        StopDropping();
        break;

    case Collecting:
        StopCollecting();
        break;

    case Delivering:
        StopDelivering();
        break;

    default:
        qDebug() << "Unknown activity, nothing stopped";
        break;
    }
}

//----------------------------------
//  Specific controls
//----------------------------------
void        CToday::StartTraveling()
{
    //  Get current time
    m_tTravelTimeStart.start();
}

void        CToday::StopTraveling()
{
    //  get time taken, or time difference
    QTime ttime = QTime( 0,0).addMSecs(m_tTravelTimeStart.elapsed());
#ifdef _Debug
    qDebug() << "Travel Time difference: " << ttime;
#endif

    //  add time to total time
    m_tTotalTravel.addSecs(ttime.second());
}

void        CToday::StartDropping()
{
    //  Get current time
    m_tDropTimeStart.start();
}

void        CToday::StopDropping()
{
    //  get time taken, or time difference
    QTime ttime = QTime( 0,0).addMSecs(m_tDropTimeStart.elapsed());
#ifdef _Debug
    qDebug() << "Travel Time difference: " << ttime;
#endif

    //  add time to total time
    m_tTotalDrop.addSecs(ttime.second());
}

void        CToday::StartCollecting()
{
    //  Get current time
    m_tCollectTimeStart.start();
}

void        CToday::StopCollecting()
{
    //  get time taken, or time difference
    QTime ttime = QTime( 0,0).addMSecs(m_tCollectTimeStart.elapsed());
#ifdef _Debug
    qDebug() << "Travel Time difference: " << ttime;
#endif

    //  add time to total time
    m_tTotalCollect.addSecs(ttime.second());
}

void        CToday::StartDelivering()
{
    //  Get current time
    m_tDeliverTimeStart = QTime::currentTime();
}

void        CToday::StopDelivering()
{
    //  get time taken, or time difference
    QTime ttime = QTime( 0,0).addMSecs(m_tDeliverTimeStart.elapsed());
#ifdef _Debug
    qDebug() << "Travel Time difference: " << ttime;
#endif

    //  add time to total time
    m_tTotalDelivery.addSecs(ttime.second());
}



//---------------------------------
//  Incrementers/Decrementers
//---------------------------------
void        CToday::AddOrdersCollected( int amount )
{
    m_iTotalOrderCollected += amount;
}

void        CToday::AddRetailValue( double famount )
{
    m_fTotalOrderAmount += famount;
}

void        CToday::AddDropped( int amount )
{
    m_iTotalDropped += amount;
}

void        CToday::AddCollected( int amount )
{
    m_iTotalCollected += amount;
}

void        CToday::AddDelivered( int amount )
{
    m_iTotalDelivered += amount;
}

void        CToday::RemoveOrdersCollected( int amount )
{
    m_iTotalOrderCollected -= amount;
}

void        CToday::RemoveRetailValue( double famount )
{
    m_fTotalOrderAmount -= famount;
}

void        CToday::RemoveDropped( int amount )
{
    m_iTotalDropped -= amount;
}

void        CToday::RemoveCollected( int amount )
{
    m_iTotalCollected -= amount;
}

void        CToday::RemoveDelivered( int amount )
{
    m_iTotalDelivered -= amount;
}

